<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chk = new CheckMode_3d($curruser['acl'], 3, $gametype);
$chk->checkShow3dPage();
$show3D3S = $chk->show3D3S;
$show5N = getShow35($gametype);
unset($chk);
$tbl = $g_gametype_3d[$gametype]['en'];
if ($curruser['acl'] == "300")
{
    $_cen_flag = 1;
}
else
{
    $_cen_flag = 0;
}
$cid = 0 - 1;
if ($_cen_flag)
{
    include_once( "centralize/centralize_class/config_cen.php" );
    include_once( "centralize/centralize_class/db_cen.php" );
    $cid = intval(getvar("cid"));
    if (!isset($g_cen_info[$cid]))
    {
        exit(" error cid ");
    }
}
require_once( "class_3d/RateConfig_3d.php" );
require_once( "class_3d/BetTotal_3d.php" );
$order = getvar("order");
$split = getvar("split");
$disk = getvar("disk");
$ratemod = getvar("ratemod");
$playtype = getvar("playtype");
$lotto_id_3d = intval(getvar("lotto_id_3d"));
$bid = getvar("bid");
$bidArr = $bid !== "" ? explode(",", $bid) : array();
$listnum = intval(getvar("listnum"));
$bnowAcl = getvar("bnowAcl");
$showSubid = getvar("subid");
$ptype = intval(getvar("ptype"));
$pnum = intval(getvar("pnum"));
$pFlag = intval(getvar("pFlag"));
if ($pFlag && $playtype == "r3")
{
    switch ($pFlag)
    {
        case 1 :
            $ptype = 0;
            $pnum = 110;
            $order = 2;
            break;
        case 2 :
            $ptype = 110;
            $pnum = 110;
            $order = 2;
            break;
    }
}
else if ($playtype == "r1pass")
{
    $order = 2;
}
if ($lotto_id_3d < 1)
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo "{\"errormsg\":\"no data\"}";
    exit();
}
$RateConfig_3d = new RateConfig_3d($gametype);
$BetTotal_3d = new BetTotal_3d($lotto_id_3d, $gametype, $cid);
$split = isset($RateConfig_3d->splitArr[$split]) ? $split : "0";
$disk = isset($RateConfig_3d->diskArr[$disk]) ? $disk : 0;
$order = isset($RateConfig_3d->orderArr[$order]) ? $order : 0;
$ratemod = isset($RateConfig_3d->ratemodArr[$ratemod]) ? $ratemod : 2;
$playtype = isset($RateConfig_3d->playtypeArr02[$playtype]) ? $playtype : "r1";
$showSubid = isset($RateConfig_3d->subArr[$showSubid]) ? $showSubid : "0";
$listnum = $listnum < 1 ? 0 : $listnum;
$time = systime() - 30;
if ($disk == 0)
{
    $diskStr = "";
}
else
{
    $diskStr = $RateConfig_3d->diskArr[$disk];
}
$sObj = NULL;
$rateArr = NULL;
$tempStr = "";
$getRatesNumArr = array();
$getRatesSNumArr = array();
$bidArrNum = count($bidArr);
if ($bid !== "" && $bidArrNum == 1 && in_array($bid, $RateConfig_3d->playGroupArr02[$playtype]))
{
    $playArr = array(
        0 => $bid
    );
}
else if (1 < $bidArrNum && count(array_intersect($RateConfig_3d->playGroupArr02[$playtype], $bidArr) == $bidArrNum))
{
    $playArr = $bidArr;
}
else
{
    $playArr = $RateConfig_3d->playGroupArr02[$playtype];
}
$acl5bnow = false;
if ($curruser['acl'] == "5" && $bnowAcl == "5")
{
    $acl5bnow = true;
}
$rArr = array();
$rWin = array();
$rAmn = array();
$rtotalArr = array();
$pgTotalAmountArr = array();
$companyFlag = true;
if ($curruser['acl'] == 255 || $curruser['acl'] == 300)
{
    $showid = "0";
    $companyFlag = false;
}
else
{
    $showid = $curruser['id'];
    $companyFlag = true;
}
if ($ratemod == 0)
{
    include( "class_3d/SRate_3d.php" );
    $SRate_3d = new SRate_3d($gametype, $cid);
    $user_id = $SRate_3d->getAcl5ID($curruser['id'], $curruser['acl']);
}
include( "class_3d/Bet_3d.php" );
$openDate = date("Y-m-d");
if ($_cen_flag)
{
    $rs = getdb_cen($cid, "SELECT open FROM lotto_{$tbl} WHERE id = {$lotto_id_3d} LIMIT 1");
}
else
{
    $rs = getdb("SELECT open FROM lotto_{$tbl} WHERE id = {$lotto_id_3d} LIMIT 1");
}
if (!$rs->eof)
{
    $openDate = $rs->f[0];
}
$rs->close();
$postTime = $conf->get("paper_post_bet");
$today = date("Hi") <= $postTime ? date("Y-m-d", strtotime("-1 day")) : date("Y-m-d");
if ($openDate == $today)
{
    $Bet_3d = new Bet_3d($gametype, "", $cid);
}
else
{
    $Bet_3d = new Bet_3d($gametype, "x", $cid);
}
$showids = $Bet_3d->getMyMemberIdByAcl($curruser['id'], $curruser['acl']);
$getDiffTotalArr = array("35", "46", "48", "49", "50", "51", "52");
$modDiffArr = array("0" => 3, "54" => 7, "55" => 5);
$showAllSubArr = array();
$rateArr = array();
$orderNumArr = array();
$orderDiffArr = array(56 => "万大小", 59 => "千大小", 4 => "百大小", 5 => "拾大小", 6 => "个大小", 57 => "万单双", 60 => "千单双", 7 => "百单双", 8 => "拾单双", 9 => "个单双", 58 => "万质合", 61 => "千质合", 10 => "百质合", 11 => "拾质合", 12 => "个质合", 46 => "一字过关", 36 => "百拾和数", 37 => "百个和数", 38 => "拾个和数", 62 => "万千和数单双", 63 => "万佰和数单双", 64 => "万拾和数单双", 65 => "万个和数单双", 66 => "千佰和数单双", 67 => "千拾和数单双", 68 => "千个和数单双", 17 => "百拾和数单双", 18 => "百个和数单双", 19 => "拾个和数单双", 39 => "百拾和数尾数", 40 => "百个和数尾数", 41 => "拾个和数尾数", 42 => "百拾个和数", 22 => "百拾个和数大小", 23 => "百拾个和数单双", 43 => "百拾个和数尾数", 44 => "百拾个和数尾数大小", 45 => "百拾个和数尾数质合", 53 => "准对", 54 => "不出牌", 71 => "总单双", 72 => "总大小", 73 => "龙虎");
foreach ($playArr as $val)
{
    if ($show5N && in_array($val, $g_n3n5Arr))
    {
        if (in_array($playtype, $showAllSubArr))
        {
            $subStart = $showSubid;
            $subEnd = $showSubid + 1;
        }
        else
        {
            $subStart = 0;
            $subEnd = 3;
        }
    }
    else
    {
        $subStart = 0;
        $subEnd = 1;
    }
    $subid = $subStart;
    while ($subid < $subEnd)
    {
        $val_subid = $val . "_" . $subid;
        if (in_array($val, $getDiffTotalArr))
        {
            $speStr = 46 < $val ? " 48,49,50,51,52 " : "";
            $tempDataTotal = $Bet_3d->getDataListTotal($lotto_id_3d, $val, $curruser['acl'], $showids, $split, $diskStr, $speStr, $subid);
        }
        else
        {
            $tempDataTotal = $BetTotal_3d->getDataListTotal($lotto_id_3d, $val_subid, $showid, $split, $diskStr);
        }
        $numbersArr = $RateConfig_3d->getBetTypeNumber($val, $pFlag, $ptype, $pnum);
        $numberstr = implode("\",\"", $numbersArr);
        $numberstr = "\"" . $numberstr . "\"";
        if ($ratemod == 0)
        {
            if ($disk == 0)
            {
                $mydisk = 1;
            }
            else
            {
                $mydisk = $disk;
            }
            $tempRateBaseArr = $SRate_3d->getRates($lotto_id_3d, $mydisk, $val, $subid);
            $getRatesNumArr[$val_subid] = $SRate_3d->getRatesNumArr($lotto_id_3d, $mydisk, $val, $subid);
            if ($tempRateBaseArr == false)
            {
                $tempRateBaseArr = array();
                $i = 0;
                while ($i < 22)
                {
                    $tempIdx = "r" . str_pad($i, 2, "0", STR_PAD_LEFT);
                    $tempRateBaseArr[$tempIdx] = 0;
                    ++$i;
                }
            }
            if ($acl5bnow)
            {
                $rateArr[$val_subid] = $tempRateBaseArr;
                $getRatesSNumArr[$val_subid] = array();
            }
            else if ($companyFlag)
            {
                if ($tempRateBaseArr2 = $SRate_3d->getRatesS($lotto_id_3d, $val, $user_id, $subid))
                {
                    $tempRateArr = array();
                    foreach ($tempRateBaseArr as $key2 => $val2)
                    {
                        $tempRateArr[$key2] = $tempRateBaseArr[$key2] - $tempRateBaseArr2[$key2];
                    }
                    $rateArr[$val_subid] = $tempRateArr;
                }
                else
                {
                    $rateArr[$val_subid] = $tempRateBaseArr;
                }
                $getRatesSNumArr[$val_subid] = $SRate_3d->getRatesSNumArr($lotto_id_3d, $user_id, $val, $subid);
            }
            else
            {
                $rateArr[$val_subid] = $tempRateBaseArr;
            }
        }
        if ($order == "1")
        {
            $orderby = "amount";
        }
        else
        {
            $orderby = "amount_rate";
        }
        if ($tempDataTotal != false)
        {
            $dataTotal = intval($tempDataTotal['totalnum']);
            $dataAmount = round($tempDataTotal['amount']);
            $databack2 = intval($tempDataTotal['back2']);
            if (isset($modDiffArr[$val]))
            {
                $databack1 = round($tempDataTotal['back1'] / $modDiffArr[$val]);
            }
            else
            {
                $databack1 = round($tempDataTotal['back1']);
            }
            if ($cutData = $RateConfig_3d->chkG3G6Idx($val))
            {
                $dataTotal = intval($dataTotal / $cutData);
                $dataAmount = round($dataAmount / $cutData);
                $databack2 = intval($databack2 / $cutData);
                $databack1 = intval($databack1 / $cutData);
            }
            else if ($val == 46)
            {
                $cutData = 3;
                $dataTotal = intval($dataTotal / $cutData);
                $dataAmount = round($dataAmount / $cutData);
                $databack2 = intval($databack2 / $cutData);
                $databack1 = intval($databack1 / $cutData);
            }
        }
        else
        {
            $dataTotal = "0";
            $dataAmount = "0";
            $databack2 = "0";
            $databack1 = "0";
        }
        $rtotalArr[] = "\"r" . $val_subid . "total\":{\"totalnum\":\"" . $dataTotal . "\",\"amount\":\"" . $dataAmount . "\"}";
        if (isset($modDiffArr[$val]))
        {
            $winlostTemp = ( $dataAmount - $databack1 ) / $modDiffArr[$val];
        }
        else
        {
            $winlostTemp = $dataAmount - $databack1;
        }
        if ($companyFlag)
        {
            $winlostTemp = $winlostTemp + $databack2;
        }
        $winlostTemp = round($winlostTemp);
        $rArr[$val_subid] = $RateConfig_3d->my_array_fill_keys($numbersArr, array(0 - 1, 0, 0, 0, $winlostTemp, ""));
        $rWin[$val_subid] = $RateConfig_3d->my_array_fill_keys($numbersArr, $winlostTemp);
        $rAmn[$val_subid] = $RateConfig_3d->my_array_fill_keys($numbersArr, 0);
        $rs = $BetTotal_3d->getDataListByNumSs($lotto_id_3d, $val, $numberstr, $showid, $diskStr, $split, $ratemod, $orderby, $listnum, $subid);
        while (!$rs->eof)
        {
            if (isset($modDiffArr[$val]))
            {
                $winlost = ( $dataAmount - $databack1 ) / $modDiffArr[$val] - $rs->f['amount_rate'];
            }
            else
            {
                $winlost = $dataAmount - $rs->f['amount_rate'] - $databack1;
            }
            if ($companyFlag)
            {
                $winlost = $winlost + $databack2;
            }
            $winlost = round($winlost);
            $totalnum = intval($rs->f['totalnum']);
            $amount = $rs->f['amount'];
            $amount = round($amount);
            $rate = floatval($rs->f['rate']);
            $numbers = (string) $rs->f['numbers'];
            $rate = floatval($rs->f['rate']);
            if ($time < $rs->f['updatetime'])
            {
                $bgclass = "namount";
            }
            else
            {
                $bgclass = "";
            }
            $rArr[$val_subid][$numbers] = array($numbers, $totalnum, $amount, $rate, $winlost, $bgclass);
            $rWin[$val_subid][$numbers] = $winlost;
            $rAmn[$val_subid][$numbers] = $amount;
            $rs->movenext();
        }
        if ($dataTotal < 1)
        {
            $orderTemp = 2;
        }
        else if (isset($orderDiffArr[$val]))
        {
            $orderTemp = 2;
        }
        else
        {
            $orderTemp = $order;
        }
        switch ($orderTemp)
        {
            case 0 :
                $tempArr = $rWin[$val_subid];
                asort($tempArr);
                reset($tempArr);
                break;
            case 1 :
                $tempArr = $rAmn[$val_subid];
                asort($tempArr);
                reset($tempArr);
                break;
            case 2 :
                $tempArr = $RateConfig_3d->my_array_fill_keys($numbersArr, 0);
                break;
        }
        $orderNumArr[$val_subid] = $tempArr;
        ++$subid;
    }
}
if (0 < $listnum)
{
    $listnumFlag = 1;
}
else
{
    $listnumFlag = 0;
}
if ($ratemod == 0)
{
    $ratemodFlag = 1;
}
else
{
    $ratemodFlag = 0;
}
$rStrArr = array();
foreach ($orderNumArr as $oKey => $oVal)
{
    $rStrTempArr = array();
    $oKeyArr = explode("_", $oKey);
    foreach ($oVal as $oKey2 => $oVal2)
    {
        $sData = $rArr[$oKey][$oKey2];
        $numTemp = $sData[0] < 0 ? $oKey2 : $sData[0];
        if ($ratemodFlag)
        {
            if (isset($SRate_3d->betForNumDiffTbl[$oKeyArr[0]]))
            {
                $numTempArr = str_split($numTemp);
                $numIdxTemp = count(array_unique($numTempArr));
                $numIdxTemp = $SRate_3d->betForNumDiffTbl[$oKeyArr[0]][$numIdxTemp];
                $numIdx2Temp = intval($numTemp);
                if (isset($getRatesNumArr[$val_subid][$numIdx2Temp]))
                {
                    $rateTemp = floatval($rateArr[$oKey][$numIdxTemp] - $getRatesNumArr[$val_subid][$numIdx2Temp]);
                }
                else
                {
                    $rateTemp = floatval($rateArr[$oKey][$numIdxTemp]);
                }
                if (isset($getRatesSNumArr[$val_subid][$numIdx2Temp]))
                {
                    $rateTemp = floatval($rateTemp - $getRatesSNumArr[$val_subid][$numIdx2Temp]);
                }
            }
            else
            {
                $rateTemp = $RateConfig_3d->getRateFromRateArr($oKeyArr[0], $numTemp, $rateArr[$oKey]);
            }
        }
        else
        {
            $rateTemp = $sData[3];
        }
        $rStrTempArr[] = "{\"numbers\":\"" . $numTemp . "\",\"totalnum\":\"" . $sData[1] . "\",\"amount\":\"" . $sData[2] . "\",\"rate\":\"" . $rateTemp . "\",\"winlost\":\"" . $sData[4] . "\",\"bgclass\":\"" . $sData[5] . "\"}";
        if ($listnumFlag && --$listnum < 1)
        {
            continue;
        }
    }
    $rStrArr[] = "\"r" . $oKey . "\":[" . implode(",", $rStrTempArr) . "]";
}
if (intval(getvar("onlinesum")))
{
    $conf = new Config();
    $member_max_limit = intval($conf->get("member_max_limit"));
    $onlinesum = 0;
    if ($playtype == "r1")
    {
        sess_gc("7200");
    }
    $rs = getdb("select count(sid) from k_sessions where uid!=2");
    if (!$rs->eof)
    {
        $onlinesum = $rs->f[0];
    }
    $rs->close();
    if ($member_max_limit < $onlinesum)
    {
        $conf->set("member_max_limit", $onlinesum);
    }
    $rStrArr[] = "\"onlinesum\":\"" . $onlinesum . "\"";
}
if (intval(getvar("useamount")))
{
    $cid = $curruser['id'];
    $sqlOpen = "SELECT open FROM lotto_{$tbl} WHERE id = {$lotto_id_3d} LIMIT 0,1";
    $openRs = getdb($sqlOpen);
    if (!$openRs->eof)
    {
        $paperOpen = $openRs->f[0];
        switch ($show3D3S)
        {
            case 0 :
                $amountField = "amount_3d";
                $tblTemp = "3d";
                $sql01 = "select sum(a.amount) as amount from bet2_{$tblTemp} a LEFT JOIN lotto_{$tblTemp} b ON (a.lotto_id = b.id) where a.user_id = {$cid} and b.open = {$paperOpen} and a.rame in (0,9)";
                $sql01 .= " UNION ALL ";
                $tblTemp = "3t";
                $sql01 = "select sum(a.amount) as amount from bet2_{$tblTemp} a LEFT JOIN lotto_{$tblTemp} b ON (a.lotto_id = b.id) where a.user_id = {$cid} and b.open = {$paperOpen} and a.rame in (0,9)";
                break;
            case 1 :
                $amountField = "amount_3s1";
                $tblTemp = "3s1";
                $sql01 = "SELECT sum(a.amount) as amount FROM bet2_{$tblTemp} a LEFT JOIN lotto_{$tblTemp} b ON (a.lotto_id=b.id) WHERE a.user_id = {$cid} AND b.open='{$paperOpen}' AND a.rame IN (0,9)";
                $sql01 .= " UNION ALL ";
                $tblTemp = "3s2";
                $sql01 .= "SELECT sum(a.amount) as amount FROM bet2_{$tblTemp} a LEFT JOIN lotto_{$tblTemp} b ON (a.lotto_id=b.id) WHERE a.user_id = {$cid} AND b.open='{$paperOpen}' AND a.rame IN (0,9)";
                $sql01 .= " UNION ALL ";
                $tblTemp = "3s3";
                $sql01 .= "SELECT sum(a.amount) as amount FROM bet2_{$tblTemp} a LEFT JOIN lotto_{$tblTemp} b ON (a.lotto_id=b.id) WHERE a.user_id = {$cid} AND b.open='{$paperOpen}' AND a.rame IN (0,9)";
                break;
        }
        $rs = getdb($sql01);
        $usedamount = 0;
        while (!$rs->eof)
        {
            $usedamount += $rs->f[0];
            $rs->next();
        }
        $rs->close();
        $users = GetUserList($curruser['id']);
        if ($users != "")
        {
            switch ($show3D3S)
            {
                case 0 :
                    $tblTemp = "3d";
                    $sql02 = "select sum(a.amount) as amount from bet_{$tblTemp} a LEFT JOIN lotto_{$tblTemp} b ON (a.lotto_id = b.id) where b.open = '{$paperOpen}' AND a.user_id in ({$users}) and a.rame in (0,9)";
                    $sql02 .= " UNION ALL ";
                    $tblTemp = "3t";
                    $sql02 .= "select sum(a.amount) as amount from bet_{$tblTemp} a LEFT JOIN lotto_{$tblTemp} b ON (a.lotto_id = b.id) where b.open = '{$paperOpen}' AND a.user_id in ({$users}) and a.rame in (0,9)";
                    break;
                case 1 :
                    $tblTemp = "3s1";
                    $sql02 = "SELECT sum(a.amount) FROM bet_{$tblTemp} a LEFT JOIN lotto_{$tblTemp} b ON (a.lotto_id = b.id) WHERE a.user_id IN ({$users}) AND b.open='{$paperOpen}' AND a.rame IN (0,9)";
                    $sql02 .= " UNION ALL ";
                    $tblTemp = "3s2";
                    $sql02 .= "SELECT sum(a.amount) FROM bet_{$tblTemp} a LEFT JOIN lotto_{$tblTemp} b ON (a.lotto_id = b.id) WHERE a.user_id IN ({$users}) AND b.open='{$paperOpen}' AND a.rame IN (0,9)";
                    $sql02 .= " UNION ALL ";
                    $tblTemp = "3s3";
                    $sql02 .= "SELECT sum(a.amount) FROM bet_{$tblTemp} a LEFT JOIN lotto_{$tblTemp} b ON (a.lotto_id = b.id) WHERE a.user_id IN ({$users}) AND b.open='{$paperOpen}' AND a.rame IN (0,9)";
            }
            $rs = getdb($sql02);
            while (!$rs->eof)
            {
                $usedamount += $rs->f[0];
                $rs->next();
            }
            $rs->close();
        }
        $usedamount = $curruser[$amountField] - $usedamount;
    }
    else
    {
        $usedamount = "--";
    }
    $openRs->close();
    $rStrArr[] = "\"useamount\":\"" . $usedamount . "\"";
}
$tempStr2 = "";
$getDiffTotalBettypesArr = array("g3", "g6", "rcmp", "r1pass", "r1all");
$r1allDataArr = array(0, 0);
foreach ($RateConfig_3d->playtypeArr02 as $key => $val)
{
    $bettypeTemp = implode(",", $RateConfig_3d->playGroupArr02[$key]);
    if (in_array($key, $getDiffTotalBettypesArr))
    {
        $speStr = implode(",", $RateConfig_3d->playGroupArr02[$key]);
        $tempDataTotal = $Bet_3d->getDataListTotal($lotto_id_3d, $speStr, $curruser['acl'], $showids, $split, $diskStr, $speStr, $key == "r1all");
    }
    else
    {
        $tempDataTotal = $BetTotal_3d->getDataListTotalForBettypeS($lotto_id_3d, $bettypeTemp, $showid, $split, $diskStr);
    }
    if ($tempDataTotal)
    {
        $tempStr2 .= ",{\"idx\":\"" . $key . "\", \"amu\":\"" . intval($tempDataTotal['amount']) . "\", \"tn\":\"" . $tempDataTotal['updatetime'] . "\"}";
        $r1allDataArr[0] += intval($tempDataTotal['amount']);
        $r1allDataArr[1] = $r1allDataArr[1] < $tempDataTotal['updatetime'] ? $tempDataTotal['updatetime'] : $r1allDataArr[1];
    }
    else
    {
        $tempStr2 .= ",{\"idx\":\"" . $key . "\", \"amu\":\"0\", \"tn\":\"0\"}";
    }
}
$tempStr2 .= ",{\"idx\":\"r1all\", \"amu\":\"" . $r1allDataArr[0] . "\", \"tn\":\"" . $r1allDataArr[1] . "\"}";
$rStrArr[] = implode(",", $rtotalArr);
$rStrArr[] = "\"pgTotalAmountStr\":[" . substr($tempStr2, 1) . "]";
header("Cache-Control: no-cache, must-revalidate");
header("Content-Type: text/plain");
echo "{" . implode(",", $rStrArr) . "}";
exit();
?>